package hhm.android.my

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.loc.dm
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject
import hhm.android.library.utils.OnMultiClickListener
import siau.android.base.INVITE_ROUTE
import siau.android.base.SBApplication
import siau.android.base.SBBaseActivity

@Route(path = INVITE_ROUTE)
class InviteFriendActivity : SBBaseActivity() {

    lateinit var weixin: LinearLayout
    lateinit var scan: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setThemeColor(0xFFAEE0F4.toInt())
        setTitleMainColorAndTextBlank()
        setContentView(R.layout.activity_invite_friend)
        initTitle(getString(R.string.invite_friend))

        val dm = applicationContext.resources.displayMetrics
        val screenWidth = dm.widthPixels

        val iv: ImageView = findViewById(R.id.activity_invite_friend_bottom_bg)
        val layoutParams = iv.layoutParams
        layoutParams.width = screenWidth
        layoutParams.height = screenWidth
        iv.layoutParams = layoutParams
        iv.setImageResource(R.mipmap.invite_bottom_bg)

        weixin = findViewById(R.id.activity_invite_friend_weixin)
        scan = findViewById(R.id.activity_invite_friend_scan)

        weixin.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {

                //初始化一个WXWebpageObject填写url
                val webpageObject = WXWebpageObject()
                webpageObject.webpageUrl = SBApplication.userData.inviteUrl
                //用WXWebpageObject对象初始化一个WXMediaMessage
                val msg = WXMediaMessage(webpageObject)
                msg.title = SBApplication.userData.shareTitle
                msg.description = SBApplication.userData.shareContent

                Glide.with(this@InviteFriendActivity).asBitmap().load(SBApplication.userData.shareImg).into(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        msg.setThumbImage(resource)

                        val req = SendMessageToWX.Req()
                        req.transaction = System.currentTimeMillis().toString()
                        req.message = msg
//                req.scene =  SendMessageToWX.Req.WXSceneTimeline
                        req.scene = SendMessageToWX.Req.WXSceneSession
                        SBApplication.api.sendReq(req)
                    }
                })
            }
        })
        scan.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                startActivity(Intent(this@InviteFriendActivity, ScanInviteActivity::class.java))
            }
        })
    }

    override fun finish() {
        setThemeColor(0xFFA7E5F7.toInt())
        super.finish()
    }
}